Thread: counting character's program issues

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    counting character's program issues

    Good day C all!
    i want to write a C program that is able to analyze a text document composed of an unknown amount of lines. The document is inserted from keyboard by the user. The program has to show the following statistics:
    a. the total number of lines;
    b. the total number of words;
    c. the number of character.

    For example, if the text provided as input is the following:
    fatti non foste
    per viver come bruti
    ma per seguir virtute e canoscenza
    the program has to print the following values:
    Number of lines: 3
    Number of words: 13
    Average length of a word: 4.5


    i've written the following code :

    Code:
    int main()
    {
    
    
    int i=0;
       int word=0; int line=0; int count=0;
        char arra[100];
    char c =getchar();
        while((c!= EOF)  )
    
    
    {
            arra[i]=c;
            count=count+1;
    
    
            if ((arra[i]==' ' || arra[i]=='\n'))
                word=word+1;
           if (arra[i]=='\n')
    
    
             ++line;
        i=i+1;
        c=getchar();
    }
    printf("the number of words is :%d ",word);
    printf("\nthe number of line is :%d",line);
    printf("\nthe number of characters is :%d",count);
    }


    My questions are:
    How can i define optimaly may array's size? i've put 100 because i had no better idea.
    can I' print the global results without typing Ctrl+Z?
    if there is any improvement for my code please show me!
    thanks!!
    Last edited by amaelle; 12-04-2016 at 11:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Counting Program. Crashes at the End.
    By psukphranee in forum C Programming
    Replies: 3
    Last Post: 05-26-2013, 11:41 PM
  2. Character counting issues from Kernighan text
    By Vampireclown in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 10:29 AM
  3. Help with a character counting program!
    By Redpred in forum C Programming
    Replies: 9
    Last Post: 08-09-2006, 08:15 AM
  4. Problem with character counting program...
    By MyglyMP2 in forum C++ Programming
    Replies: 9
    Last Post: 03-25-2005, 05:12 PM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM

Tags for this Thread